import time

import scrapy

class ImgInfoItem(scrapy.Item):
    name = scrapy.Field()
    url = scrapy.Field()
    width = scrapy.Field()
    height = scrapy.Field()

def get_url(page):
    if page == 1:
        return "https://wallhaven.cc/toplist"
    else:
        return f"https://wallhaven.cc/toplist?page={page}"

class WallhavenSpider(scrapy.Spider):
    name = "wallhaven"

    start_urls = [get_url(x) for x in range(1,4)]


    def parse(self, response, **kwargs):
        img_links = response.css(".preview::attr(href)").getall()
        for link in img_links:
            yield scrapy.Request(link,callback=self.parse_img)

    def parse_img(self,reponse):
        item = ImgInfoItem()
        item["url"] = reponse.css(".scrollbox img::attr(src)").extract()[0]
        item["name"] = reponse.css(".scrollbox img::attr(data-wallpaper-id)").extract()[0]
        item["width"] = int(reponse.css(".scrollbox img::attr(data-wallpaper-width)").extract()[0])
        item["height"] = int(reponse.css(".scrollbox img::attr(data-wallpaper-height)").extract()[0])
        yield item
